package com.nyt.org.modules.BusPlan.service;

import com.alibaba.fastjson.JSONObject;
import com.nyt.org.common.utils.SendPostRequest;
import com.nyt.org.common.utils.xml.XmlUtils;
import com.nyt.org.modules.BusPlan.model.REQ2004;
import com.nyt.org.modules.BusPlan.model.REQBODY2004;
import com.nyt.org.modules.BusPlan.model.REQROOT2004;
import com.nyt.org.modules.BusPlan.model.RSP2004ROOT;
import com.nyt.org.modules.api.constant.ZhongLvCode;
import com.nyt.org.modules.api.model.XMLHead;
import com.nyt.org.modules.TicketStaion.service.FaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 车次信息查询 [2004]
 *
 * */
@Service
public class BusPlanQueryService extends FaService {

    private Logger logger = LoggerFactory.getLogger(BusPlanQueryService.class);

    @Value("${zhonglv.server}")
    private String zhonglvURL;

    public  Map<String,Object> getBusPlan(JSONObject bodyJson) {

        Map<String,Object> map = new HashMap<>();

        REQROOT2004 root = new REQROOT2004();

        XMLHead head = super.bulldHEADXml(ZhongLvCode.车次信息查询);
        root.setHead(head);

        REQBODY2004 body = new REQBODY2004();
        REQ2004 req2004 = new REQ2004();
        req2004.setDepartureCityNo(bodyJson.getString("DepartureCityNo"));
        req2004.setArrivalCityNo(bodyJson.getString("ArrivalCityNo"));
        req2004.setDepartureDate(bodyJson.getString("DepartureDate"));//格式 YYYYMMDD
        req2004.setTimePart(bodyJson.getString("TimePart"));
        req2004.setOnStationCode(bodyJson.getString("OnStationCode"));
        req2004.setOffStationCode(bodyJson.getString("OffStationCode"));
        req2004.setOrgNo(bodyJson.getString("orgNo"));
        body.setReq2004(req2004);

        root.setBody(body);

        String reqXml = XmlUtils.toXml(root);

        logger.info("车次信息查询[2004]请求报文：\n"+reqXml);

        String resXml = SendPostRequest.doPost(zhonglvURL,reqXml);
        logger.info("车次信息查询[2004]响应报文：\n"+resXml);

        // xml转bean
        RSP2004ROOT respROOT = XmlUtils.xmlToBean(resXml, RSP2004ROOT.class);

        map.put("rspCode",respROOT.getHead().getRspCode());
        map.put("rspMsg",respROOT.getHead().getRspMsg());
        map.put("responseBean",respROOT);

        return map;
    }




}
